﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsDMLienHeDoiTuong
    {
        private string mChucDanh = "";
        private string mDiaChi = "";
        private string mDienThoai = "";
        private string mEmail = "";
        private string mKhoa = "";
        private string mKhoaDoiTuong = "";
        private string mTen = "";
        private int mTruongDaiDien = 0;

        public bool Delete(string pKhoa, string pKhoaDoiTuong)
        {
            IDataParameter parameter = null;
            bool flag=false;
            try
            {
                string commandText = " DELETE FROM DM_LienHeDoiTuong WHERE Khoa = '" + pKhoa + "' and KhoaDoiTuong = '" + pKhoaDoiTuong + "'";
                bool flag2 = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }) > 0;
                flag = flag2;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable GetList(string pKhoaDoiTuong = "")
        {
            IDataParameter parameter = null;
            DataTable table;
            try
            {
                string commandText = "SELECT *  FROM DM_LienHeDoiTuong  WHERE KhoaDoiTuong = '" + pKhoaDoiTuong + "' ORDER BY Ten";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListField(string pField, string Conditions = "", string Order = "")
        {
            DataTable table;
            string str = "";
            string str2 = "";
            pField = pField.Replace("|", " , ");
            IDataParameter parameter = null;
            if (Conditions.Trim() != "")
            {
                str2 = " WHERE " + Conditions;
            }
            if (Order.Trim() != "")
            {
                str = " ORDER BY " + Order;
            }
            try
            {
                string commandText = "SELECT " + pField + " FROM DM_LienHeDoiTuong " + str2 + str;
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool isDupplicateCode(string pKhoa, string pKhoaDoiTuong)
        {
            IDataParameter parameter = null;
            bool flag=false;
            try
            {
                string commandText = "SELECT * FROM DM_LienHeDoiTuong WHERE Rtrim(KhoaDoiTuong) = '" + pKhoaDoiTuong.Trim() + "' And Rtrim(Khoa) <> '" + pKhoa.Trim() + "'";
                DataTable table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
                if (table.Rows.Count > 0)
                {
                    return true;
                }
                flag = false;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[4];
            try
            {
                string commandText = "SELECT * FROM DM_LienHeDoiTuong WHERE Khoa = '" + pKhoa + "' ";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, commandParameters);
                if (dataReader.Read())
                {
                    this.mKhoa = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDoiTuong = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mTen = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mChucDanh = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                    this.mDienThoai = Conversions.ToString(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetString));
                    this.mEmail = Conversions.ToString(modGeneral.GetValue(dataReader, 5, modGeneral.typeOfColumn.GetString));
                    this.mDiaChi = Conversions.ToString(modGeneral.GetValue(dataReader, 6, modGeneral.typeOfColumn.GetString));
                    this.mTruongDaiDien = Conversions.ToInteger(modGeneral.GetValue(dataReader, 7, modGeneral.typeOfColumn.GetInt32));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag2;
            IDataParameter[] commandParameters = new IDataParameter[8];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@KhoaDoiTuong", DbType.Int64, ParameterDirection.Input, this.mKhoaDoiTuong);
                commandParameters[2] = DBHelper.createParameter("@Ten", DbType.Int64, ParameterDirection.Input, this.mTen);
                commandParameters[3] = DBHelper.createParameter("@ChucDanh", DbType.Int64, ParameterDirection.Input, this.mChucDanh);
                commandParameters[4] = DBHelper.createParameter("@DienThoai", DbType.Int64, ParameterDirection.Input, this.mDienThoai);
                commandParameters[5] = DBHelper.createParameter("@Email", DbType.Int64, ParameterDirection.Input, this.mEmail);
                commandParameters[6] = DBHelper.createParameter("@DiaChi", DbType.Int64, ParameterDirection.Input, this.mDiaChi);
                commandParameters[7] = DBHelper.createParameter("@TruongDaiDien", DbType.Double, ParameterDirection.Input, this.mTruongDaiDien);
                bool flag = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "sp_DM_LienHeDoiTuong", commandParameters) > 0;
                flag2 = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag2 = false;
                ProjectData.ClearProjectError();
                return flag2;
                ProjectData.ClearProjectError();
            }
            return flag2;
        }

        public void SetBlank()
        {
            this.mKhoa = "";
            this.mKhoaDoiTuong = "";
            this.mTen = "";
            this.mChucDanh = "";
            this.mDienThoai = Conversions.ToString(0);
            this.mEmail = "";
            this.mDiaChi = "";
            this.mTruongDaiDien = 0;
        }

        public DataTable ShowAllList()
        {
            DataTable table;
            string commandText = "";
            IDataParameter parameter = null;
            try
            {
                commandText = " SELECT Khoa, KhoaDoiTuong,  Rtrim(Ten) as Ten  FROM DM_LienHeDoiTuong ORDER BY Ten ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable ShowList(string strKeyFilter = "", string strFiledNameFilter = "")
        {
            DataTable table;
            string commandText = "";
            IDataParameter parameter = null;
            string str3 = "";
            string str = "";
            try
            {
                if (strKeyFilter.Trim() != "")
                {
                    object instance = Strings.Split(strKeyFilter, "|", -1, CompareMethod.Binary);
                    int num2 = Information.UBound((Array) instance, 1);
                    for (int i = 0; i <= num2; i++)
                    {
                        if (str.Trim() == "")
                        {
                            str = Conversions.ToString(Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject(Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject("'", NewLateBinding.LateIndexGet(instance, new object[] { i }, null)), "'"));
                        }
                        else
                        {
                            str = Conversions.ToString(Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject(Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject(str + ",'", NewLateBinding.LateIndexGet(instance, new object[] { i }, null)), "'"));
                        }
                    }
                    str3 = " AND " + strFiledNameFilter + " IN (" + str + ")";
                }
                commandText = " SELECT Khoa,  Rtrim(Ten" + modGeneral.H_LANGUAGE + ") as Ten  FROM DM_LienHeDoiTuong WHERE  " + str3 + " ORDER BY Ten ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public string ChucDanh
        {
            get
            {
                return this.mChucDanh;
            }
            set
            {
                this.mChucDanh = value;
            }
        }

        public string DiaChi
        {
            get
            {
                return this.mDiaChi;
            }
            set
            {
                this.mDiaChi = value;
            }
        }

        public string DienThoai
        {
            get
            {
                return this.mDienThoai;
            }
            set
            {
                this.mDienThoai = value;
            }
        }

        public string Email
        {
            get
            {
                return this.mEmail;
            }
            set
            {
                this.mEmail = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string KhoaDoiTuong
        {
            get
            {
                return this.mKhoaDoiTuong;
            }
            set
            {
                this.mKhoaDoiTuong = value;
            }
        }

        public string Ten
        {
            get
            {
                return this.mTen;
            }
            set
            {
                this.mTen = value;
            }
        }

        public int TruongDaiDien
        {
            get
            {
                return this.mTruongDaiDien;
            }
            set
            {
                this.mTruongDaiDien = value;
            }
        }
    }
}

